import gifAnimation.*;
import SimpleOpenNI.*;
SimpleOpenNI kinect;

ArrayList<PVector> handPositions;

PVector currentHand;
PVector previousHand;

int x;
int y;
PImage[] bgAnimation;
Gif bgImg;
int count;
int xp;
int yp;
boolean confirmImage = true;

void setup(){
  size (945,538);
  frameRate(100);
  bgImg = new Gif(this, "earth-hands-12.gif");
  bgImg.play();
 
  kinect = new SimpleOpenNI(this);
  kinect.setMirror(true);
  
  kinect.enableDepth();
  kinect.enableGesture();
  kinect.enableHands();
  
  kinect.addGesture("RaiseHand");
  handPositions = new ArrayList();
  
  stroke(0,0,0);
  strokeWeight(2);
}

void draw(){
  kinect.update();
 background(bgImg);
 //image(kinect.depthImage(),0,0);
  
  count = 0;
  for(int i=1;i< handPositions.size(); i++) 
  {
    currentHand = handPositions.get(i);
    previousHand = handPositions.get(i-1);
    //fill(255,0,0);
    //ellipse(previousHand.x,previousHand.y,15,15);
    x = int(currentHand.x);
    y = int(currentHand.y);
    xp = int(previousHand.x);
    yp = int(previousHand.y);
    
    // this checks that the hand position is between the values (318,373) and (338,393), you should change these values according to the position where the clicking occurs
    if (x > 465 && x < 495 && y > 380 && y < 415 && xp > 465 && xp < 495 && yp > 380 && yp < 415)
    {
     count++;
     // this changes the background to black (represented by the value zero), you can replace this with an image let's say the image is in a variable called newIMG, then you would write background(newIMG)
     if (confirmImage) {
       fill(0,0,0);
       rectMode(CENTER);
       rect(480,400,200,100);}
     else background(bgImg);
     confirmImage = !confirmImage; 
    }
    else background(bgImg);
    
    if (count >= 100) background(0);

    //ellipse(x,y,15,15);
  }
  println("x="+x);
  println("y="+y);
  fill(255,255,255);
  ellipse(x,y,15,15);

  }

void onCreateHands(int handId, PVector position, float time){
  kinect.convertRealWorldToProjective(position,position);
  handPositions.add(position);
}

void onUpdateHands(int handId, PVector position, float time){
  kinect.convertRealWorldToProjective(position, position);
  handPositions.add(position);
}

void onDestroyHands(int handId, float time) {
  handPositions.clear();
  kinect.addGesture("RaiseHand");
}

void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition) {
  kinect.startTrackingHands(endPosition);
  kinect.removeGesture("RaiseHand");
}


